﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using BasicForms;
using Lists;
using DevExpress.XtraPrinting;
using System.Collections;
using CustomSecurity;

namespace ReportGenerators.AccoutingXaLa
{
    public partial class FrmBangCanDoiKeToan : BasicForms.FrmSupportPrintableComponents
    {
        public FrmBangCanDoiKeToan()
        {
            InitializeComponent();
        }

        AccoutingXaLa.DsAccountingXaLa dsDetail = new AccoutingXaLa.DsAccountingXaLa();

        private void FrmBangCanDoiKeToan_Load(object sender, EventArgs e)
        {
            if (DesignMode) return;

            cmbKyBaoCao.SelectedIndex = 19;

            FormatNumberInGridView(gridView);
            foreach (DevExpress.XtraGrid.Columns.GridColumn column in gridView.Columns)
            {
                column.OptionsFilter.AutoFilterCondition = DevExpress.XtraGrid.Columns.AutoFilterCondition.Contains;
                //if(column.Name != "colSo" || column.Name != "colTenDoiTuongNghiepVu")
                //    column.OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.False;
            }
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            BasicForms.FrmProgressBarDev.StartShowProgress();
            SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);
            dsDetail.Detail_BangCanDoiKeToan.Clear();
            
            int stt = 1;
            try
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand();

                dsDetail.Detail_BangCanDoiKeToan.Clear();
                FillDataFixFormatToTable();

                foreach (DataRow row in dsDetail.Detail_BangCanDoiKeToan.Rows)
                {
                    Calculate(cmd, row);
                }

                gridControl.DataSource = dsDetail.Detail_BangCanDoiKeToan;
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
            finally
            {
                con.Close();
                con = null;
                FormatNumberInGridView(gridView);
            }
            BasicForms.FrmProgressBarDev.FinishShowProgress();
        }

        private void FillDataFixFormatToTable()
        {
            try
            {
                //row1
                DataRow row1 = dsDetail.Detail_BangCanDoiKeToan.NewRow();
                row1["STT1"] = "A";
                row1["TenTaiSan"] = "Tài sản lưu động";
                row1["SoHieu1"] = "";
                row1["STT2"] = "A";
                row1["TenNguonVon"] = "Nợ phải trả";
                row1["SoHieu2"] = "";
                dsDetail.Detail_BangCanDoiKeToan.Rows.Add(row1);

                //row2
                DataRow row2 = dsDetail.Detail_BangCanDoiKeToan.NewRow();
                row2["STT1"] = "I";
                row2["TenTaiSan"] = "Tiền";
                row2["SoHieu1"] = "";
                row2["STT2"] = "";
                row2["TenNguonVon"] = "- Trả cho xã viên, người bán";
                row2["SoHieu2"] = "3311";
                dsDetail.Detail_BangCanDoiKeToan.Rows.Add(row2);

                //row3
                DataRow row3 = dsDetail.Detail_BangCanDoiKeToan.NewRow();
                row3["STT1"] = "";
                row3["TenTaiSan"] = "- Tiền mặt";
                row3["SoHieu1"] = "1111";
                row3["STT2"] = "";
                row3["TenNguonVon"] = "- Trả khác";
                row3["SoHieu2"] = "3312";
                dsDetail.Detail_BangCanDoiKeToan.Rows.Add(row3);

                //row4
                DataRow row4 = dsDetail.Detail_BangCanDoiKeToan.NewRow();
                row4["STT1"] = "";
                row4["TenTaiSan"] = "- Tiền gửi";
                row4["SoHieu1"] = "1121";
                row4["STT2"] = "B";
                row4["TenNguonVon"] = "Nguồn vốn chủ sở hữu";
                row4["SoHieu2"] = "";
                dsDetail.Detail_BangCanDoiKeToan.Rows.Add(row4);

                //row5
                DataRow row5 = dsDetail.Detail_BangCanDoiKeToan.NewRow();
                row5["STT1"] = "II";
                row5["TenTaiSan"] = "Đầu tư tài chính ngắn hạn";
                row5["SoHieu1"] = "1211";
                row5["STT2"] = "";
                row5["TenNguonVon"] = "- Vốn kinh doanh";
                row5["SoHieu2"] = "411";
                dsDetail.Detail_BangCanDoiKeToan.Rows.Add(row5);

                //row6
                DataRow row6 = dsDetail.Detail_BangCanDoiKeToan.NewRow();
                row6["STT1"] = "III";
                row6["TenTaiSan"] = "Các khoản thu";
                row6["SoHieu1"] = "";
                row6["STT2"] = "";
                row6["TenNguonVon"] = "- Các loại quỹ";
                row6["SoHieu2"] = "";
                dsDetail.Detail_BangCanDoiKeToan.Rows.Add(row6);

                //row7
                DataRow row7 = dsDetail.Detail_BangCanDoiKeToan.NewRow();
                row7["STT1"] = "";
                row7["TenTaiSan"] = "- Thu các hộ";
                row7["SoHieu1"] = "1311";
                row7["STT2"] = "";
                row7["TenNguonVon"] = "   + Quỹ P.lợi+ K.thưởng";
                row7["SoHieu2"] = "3532";
                dsDetail.Detail_BangCanDoiKeToan.Rows.Add(row7);

                //row8
                DataRow row8 = dsDetail.Detail_BangCanDoiKeToan.NewRow();
                row8["STT1"] = "";
                row8["TenTaiSan"] = "- Thu khác (tạm ứng)";
                row8["SoHieu1"] = "1312";
                row8["STT2"] = "";
                row8["TenNguonVon"] = "   + Quỹ rủi ro, PTSX";
                row8["SoHieu2"] = "418";
                dsDetail.Detail_BangCanDoiKeToan.Rows.Add(row8);

                //row9
                DataRow row9 = dsDetail.Detail_BangCanDoiKeToan.NewRow();
                row9["STT1"] = "IV";
                row9["TenTaiSan"] = "Hàng tồn kho";
                row9["SoHieu1"] = "";
                row9["STT2"] = "";
                row9["TenNguonVon"] = "- Lãi chưa phân phối";
                row9["SoHieu2"] = "421";
                dsDetail.Detail_BangCanDoiKeToan.Rows.Add(row9);

                //row10
                DataRow row10 = dsDetail.Detail_BangCanDoiKeToan.NewRow();
                row10["STT1"] = "";
                row10["TenTaiSan"] = "- Vật liệu";
                row10["SoHieu1"] = "152";
                row10["STT2"] = "";
                row10["TenNguonVon"] = "- Doanh thu chưa KC";
                row10["SoHieu2"] = "511";
                dsDetail.Detail_BangCanDoiKeToan.Rows.Add(row10);

                //row11
                DataRow row11 = dsDetail.Detail_BangCanDoiKeToan.NewRow();
                row11["STT1"] = "";
                row11["TenTaiSan"] = "- CP DV dở dang";
                row11["SoHieu1"] = "154";
                row11["STT2"] = "";
                row11["TenNguonVon"] = "";
                row11["SoHieu2"] = "";
                dsDetail.Detail_BangCanDoiKeToan.Rows.Add(row11);

                //row12
                DataRow row12 = dsDetail.Detail_BangCanDoiKeToan.NewRow();
                row12["STT1"] = "V";
                row12["TenTaiSan"] = "Tài sản lưu động khác";
                row12["SoHieu1"] = "";
                row12["STT2"] = "";
                row12["TenNguonVon"] = "";
                row12["SoHieu2"] = "";
                dsDetail.Detail_BangCanDoiKeToan.Rows.Add(row12);

                //row13
                DataRow row13 = dsDetail.Detail_BangCanDoiKeToan.NewRow();
                row13["STT1"] = "";
                row13["TenTaiSan"] = "- Chi phí trả trước";
                row13["SoHieu1"] = "242";
                row13["STT2"] = "";
                row13["TenNguonVon"] = "";
                row13["SoHieu2"] = "";
                dsDetail.Detail_BangCanDoiKeToan.Rows.Add(row13);

                //row14
                DataRow row14 = dsDetail.Detail_BangCanDoiKeToan.NewRow();
                row14["STT1"] = "";
                row14["TenTaiSan"] = "- Chi phí quản lý DD";
                row14["SoHieu1"] = "642";
                row14["STT2"] = "";
                row14["TenNguonVon"] = "";
                row14["SoHieu2"] = "";
                dsDetail.Detail_BangCanDoiKeToan.Rows.Add(row14);

                //row15
                DataRow row15 = dsDetail.Detail_BangCanDoiKeToan.NewRow();
                row15["STT1"] = "";
                row15["TenTaiSan"] = "- Chi phí SXKD DD";
                row15["SoHieu1"] = "631";
                row15["STT2"] = "";
                row15["TenNguonVon"] = "";
                row15["SoHieu2"] = "";
                dsDetail.Detail_BangCanDoiKeToan.Rows.Add(row15);

                //row16
                DataRow row16 = dsDetail.Detail_BangCanDoiKeToan.NewRow();
                row16["STT1"] = "B";
                row16["TenTaiSan"] = "TSCĐ";
                row16["SoHieu1"] = "";
                row16["STT2"] = "";
                row16["TenNguonVon"] = "";
                row16["SoHieu2"] = "";
                dsDetail.Detail_BangCanDoiKeToan.Rows.Add(row16);

                //row17
                DataRow row17 = dsDetail.Detail_BangCanDoiKeToan.NewRow();
                row17["STT1"] = "";
                row17["TenTaiSan"] = "- TSCĐ";
                row17["SoHieu1"] = "";
                row17["STT2"] = "";
                row17["TenNguonVon"] = "";
                row17["SoHieu2"] = "";
                dsDetail.Detail_BangCanDoiKeToan.Rows.Add(row17);

                //row18
                DataRow row18 = dsDetail.Detail_BangCanDoiKeToan.NewRow();
                row18["STT1"] = "";
                row18["TenTaiSan"] = "   + Nguyên giá";
                row18["SoHieu1"] = "211";
                row18["STT2"] = "";
                row18["TenNguonVon"] = "";
                row18["SoHieu2"] = "";
                dsDetail.Detail_BangCanDoiKeToan.Rows.Add(row18);

                //row19
                DataRow row19 = dsDetail.Detail_BangCanDoiKeToan.NewRow();
                row19["STT1"] = "";
                row19["TenTaiSan"] = "   + Hao mòn (ghi đỏ)";
                row19["SoHieu1"] = "214";
                row19["STT2"] = "";
                row19["TenNguonVon"] = "";
                row19["SoHieu2"] = "";
                dsDetail.Detail_BangCanDoiKeToan.Rows.Add(row19);

                //row20
                DataRow row20 = dsDetail.Detail_BangCanDoiKeToan.NewRow();
                row20["STT1"] = "";
                row20["TenTaiSan"] = "   + Góp vốn";
                row20["SoHieu1"] = "";
                row20["STT2"] = "";
                row20["TenNguonVon"] = "";
                row20["SoHieu2"] = "";
                dsDetail.Detail_BangCanDoiKeToan.Rows.Add(row20);

                //row21
                DataRow row21 = dsDetail.Detail_BangCanDoiKeToan.NewRow();
                row21["STT1"] = "";
                row21["TenTaiSan"] = "   + Cho vay vốn";
                row21["SoHieu1"] = "";
                row21["STT2"] = "";
                row21["TenNguonVon"] = "";
                row21["SoHieu2"] = "";
                dsDetail.Detail_BangCanDoiKeToan.Rows.Add(row21);

                //row22
                DataRow row22 = dsDetail.Detail_BangCanDoiKeToan.NewRow();
                row22["STT1"] = "";
                row22["TenTaiSan"] = "- XDCB DD";
                row22["SoHieu1"] = "241";
                row22["STT2"] = "";
                row22["TenNguonVon"] = "";
                row22["SoHieu2"] = "";
                dsDetail.Detail_BangCanDoiKeToan.Rows.Add(row22);

                ////row23
                //DataRow row23 = dsDetail.Detail_BangCanDoiKeToan.NewRow();
                //row23["STT1"] = "";
                //row23["TenTaiSan"] = "";
                //row23["SoHieu1"] = "";
                //row23["STT2"] = "";
                //row23["TenNguonVon"] = "";
                //row23["SoHieu2"] = "";
                //dsDetail.Detail_BangCanDoiKeToan.Rows.Add(row23);
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
        }

        private void Calculate(SqlCommand cmd, DataRow row)
        {
            try
            {
                SoDuTaiKhoan soDuBanDau1 = null;
                SoDuTaiKhoan soDuCuoiKy1 = null;
                SoDuTaiKhoan soDuBanDau2 = null;
                SoDuTaiKhoan soDuCuoiKy2 = null;
                TaiKhoan tk1 = null, tk2 = null;

                if(row != null && row["SoHieu1"] is string)
                    tk1 = DanhMucTaiKhoan.Instance.GetBySoHieu(row["SoHieu1"].ToString());
                if (row != null && row["SoHieu2"] is string)
                    tk2 = DanhMucTaiKhoan.Instance.GetBySoHieu(row["SoHieu2"].ToString());

                if (tk1 != null)
                {
                    soDuBanDau1 = SoDuTaiKhoan.CalculateFromDB(cmd, dtNgayBatDau.DateTime, tk1.ID);
                    soDuCuoiKy1 = SoDuTaiKhoan.CalculateFromDB(cmd, dtNgayKetThuc.DateTime, tk1.ID);
                }

                if (tk2 != null)
                {
                    soDuBanDau2 = SoDuTaiKhoan.CalculateFromDB(cmd, dtNgayBatDau.DateTime, tk2.ID);
                    soDuCuoiKy2 = SoDuTaiKhoan.CalculateFromDB(cmd, dtNgayKetThuc.DateTime, tk2.ID);
                }

                if (soDuBanDau1 != null)
                    if (soDuBanDau1.DuNo > 0)
                        row["DuDauKy1"] = soDuBanDau1.DuNo;
                    else
                        row["DuDauKy1"] = soDuBanDau1.DuCo;

                if (soDuCuoiKy1 != null)
                    if (soDuCuoiKy1.DuNo > 0)
                        row["DuCuoiKy1"] = soDuCuoiKy1.DuNo;
                    else
                        row["DuCuoiKy1"] = soDuCuoiKy1.DuCo;

                if (soDuBanDau2 != null)
                    if (soDuBanDau2.DuNo > 0)
                        row["DuDauKy2"] = soDuBanDau2.DuNo;
                    else
                        row["DuDauKy2"] = soDuBanDau2.DuCo;

                if (soDuCuoiKy2 != null)
                    if (soDuCuoiKy2.DuNo > 0)
                        row["DuCuoiKy2"] = soDuCuoiKy2.DuNo;
                    else
                        row["DuCuoiKy2"] = soDuCuoiKy2.DuCo;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void cmbKyBaoCao_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                FrmThamSoBaoCao.ThietLapKhoangThoiGianBaoCao(cmbKyBaoCao.Text, cmbKyBaoCao, dtNgayBatDau, dtNgayKetThuc);
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            //foreach (DataRow row in dsDetail.Detail_BangCanDoiKeToan.Rows)
            //{
            //    foreach (DataColumn col in dsDetail.Detail_BangCanDoiKeToan.Columns)
            //    {
            //        if (row[col] is string && row[col].ToString().Contains(""))
            //            row[col] = row[col].ToString().Replace("", "");

            //        if (row[col] is string && row[col].ToString().Contains(" [ITALIC]"))
            //            row[col] = row[col].ToString().Replace(" [ITALIC]", "");

            //        if (row[col] is string && row[col].ToString().Contains(""))
            //            row[col] = row[col].ToString().Replace("", "");
            //    }
            //}

            string year = "";
            if (dtNgayBatDau.DateTime.Year == dtNgayBatDau.DateTime.Year) year = dtNgayBatDau.DateTime.Year.ToString();
            else year = dtNgayBatDau.DateTime.Year.ToString() + dtNgayKetThuc.DateTime.Year.ToString();

            string curUSer = "";
            if (User.CurUser.FullName != null)
                curUSer = User.CurUser.FullName;

            PrintWithHeaderAnd4Footer(true, gridControl, true, true, string.Format("BẢNG CÂN ĐỐI KẾ TOÁN NĂM {0}", year), "Đơn vị tính: Đồng", "RIGHT", "", "", 2,
                "Người lập biểu", curUSer, string.Format("Hà Nội, ngày {0} tháng {1} năm {2}",
                DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year), "HTXNN XA LA");

           
        }

        //Tô đậm và in nghiêng những đoạn cần
        private void gridView_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            //if (e.CellValue != null && e.CellValue.ToString().Contains(""))
            //{
            //    e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
            //    dsDetail.Detail_BangCanDoiKeToan.Rows[e.RowHandle][e.Column.ColumnHandle] = e.CellValue.ToString().Replace("", "");
            //    //e.DisplayText = e.CellValue.ToString().Replace("", "");
            //}
            //if (e.CellValue != null && e.CellValue.ToString().Contains(" [ITALIC]"))
            //{
            //    dsDetail.Detail_BangCanDoiKeToan.Rows[e.RowHandle][e.Column.ColumnHandle] = e.CellValue.ToString().Replace(" [ITALIC]", "");
            //    //e.DisplayText = e.CellValue.ToString().Replace(" [ITALIC]", "");
            //    e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Italic);
            //}
            //if (e.CellValue != null && e.CellValue.ToString().Contains(""))
            //{
            //    dsDetail.Detail_BangCanDoiKeToan.Rows[e.RowHandle][e.Column.ColumnHandle] = e.CellValue.ToString().Replace("", "");
            //    //e.DisplayText = e.CellValue.ToString().Replace("", "");
            //    e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Underline);
            //}

            //in chữ đậm
            if (e.Column.ColumnHandle == 0 || e.Column.ColumnHandle == 1)
            {
                if (e.RowHandle == 0 || e.RowHandle == 15)
                    e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);

            }

            if (e.Column.ColumnHandle == 5 || e.Column.ColumnHandle == 6)
            {
                if (e.RowHandle == 0 || e.RowHandle == 3)
                    e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);

            }

            //chữ gạch chân
            if (e.Column.ColumnHandle == 1)
            {
                if (e.RowHandle == 1 || e.RowHandle == 4 || e.RowHandle == 5 || e.RowHandle == 8 || e.RowHandle == 11)
                {
                    e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Underline);
                }
            }
        }
    }
}
